﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;

namespace YouHP.Pages.Profile
{
    public class WeightAjax : BasePage
    {
        private int showMode = -1;

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            if(IsPost == false) HandleGet();
        }

        protected void HandleGet()
        {
            int id = RequestTools.GetQueryInt("id", 0); 
            if (id <= 0) return;
            showMode = RequestTools.GetQueryInt("showMode", Constants.DEFAULTMODE);

            PatientRecordInfo prData = PatientBLL.Instance.GetPatientRecordInfo(id);
            if (prData.Id <= 0) return;
            
            List<WeightHistoryInfo> lst = null;
            double height = PatientBLL.Instance.GetPatientHeight(id);
            if (height >= 0)
            {
                lst = PatientBLL.Instance.GetWeightHistoryList(prData.Id, Constants.ALLMODE);
            }

            MakeWeightGraphData(prData, height,lst);
        }

        private void MakeWeightGraphData(PatientRecordInfo prData, double height,List<WeightHistoryInfo> lst)
        {
            string tmpTimeStr = "&Time=", tmpValue1Str = "&Value1=", tmpValue2Str = "&Value2=";
            
            StringBuilder sb = new StringBuilder();
            sb.Append("&Weight=1&\r\n");
            sb.AppendFormat("&ShowMode={0}&\r\n", showMode);
            sb.AppendFormat("&StartDate={0:yyyy-MM-dd}&\r\n", prData.StartDate);
            sb.AppendFormat("&DosageDate={0:yyyy-MM-dd}&\r\n", prData.ConfirmDate);
            sb.AppendFormat("&UpdateDate={0:yyyy-MM-dd}&\r\n", prData.UpdateDate);

            if (height > 0)
            {
                sb.AppendFormat("&Var=Weight;{0}&\r\n", lst.Count);
                for (int i = 0; lst != null && i < lst.Count; i++)
                {
                    WeightHistoryInfo info = lst[i];
                    tmpTimeStr += string.Format("{0}", info.RecordDate.ToString("yyyy-MM-dd"));
                    if (i < lst.Count - 1)
                    {
                        tmpTimeStr += ";";
                    }

                    tmpValue1Str += string.Format("{0}", info.DataValue);
                    if (i < lst.Count - 1)
                    {
                        tmpValue1Str += ";";
                    }

                    tmpValue2Str += string.Format("{0:0.#}", info.DataValue / (height * height));
                    if (i < lst.Count - 1)
                    {
                        tmpValue2Str += ";";
                    }
                }
            }
            else
            {
                sb.AppendFormat("&Var=Weight;0&\r\n");  //无效的体重信息，因为没有身高
            }

            tmpTimeStr += "&\r\n";
            tmpValue1Str += "&\r\n";
            tmpValue2Str += "&\r\n";

            sb.AppendFormat(tmpTimeStr);
            sb.AppendFormat(tmpValue1Str);
            sb.AppendFormat(tmpValue2Str);

            ResponseXML(sb);
            
        }

        private void ResponseXML(StringBuilder sb)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "Text/html";
            System.Web.HttpContext.Current.Response.Expires = 0;

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }
    }
}